package com.hang.util.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hang.mapper.ClazzMapper;
import com.hang.model.Clazz;
import com.hang.vo.ClazzVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;

@Slf4j
public class ClazzListener extends AnalysisEventListener<ClazzVo> {
    private ClazzMapper clazzMapper;

    public ClazzListener(ClazzMapper clazzMapper) {
        this.clazzMapper = clazzMapper;
    }

    //一行一行读取
    @Override
    public void invoke(ClazzVo clazzVo, AnalysisContext analysisContext) {
        if(!ObjectUtils.isEmpty(clazzVo.getClazzId())){
            //调用方法添加数据库
            Clazz clazz = new Clazz();
            BeanUtils.copyProperties(clazzVo,clazz);
            clazzMapper.insert(clazz);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("班级信息导入完成");
    }
}